package models

import (
	"fmt"
	"time"

	"github.com/astaxie/beego/orm"
)

type Article struct {
	Id       string    `orm:"column(id);pk"`
	Name     string    `orm:"column(name)"`
	Locate   string    `orm:"column(locate)"`
	Remark   string    `orm:"column(remark);null"`
	Source   string    `orm:"column(source);null"`
	Banner   string    `orm:"column(banner);null"`
	Top      int       `orm:"column(top)"`
	Menu     *Menu     `orm:"rel(fk);column(menu_id)"`
	Status   int       `orm:"column(status)"`
	IsDelete int       `orm:"column(isdelete)"`
	Ctime    time.Time `orm:"column(ctime);type(timestamp without time zone)"`
	Creater  string    `orm:"column(creater)"`
	Utime    time.Time `orm:"column(utime);type(timestamp without time zone)"`
	Updater  string    `orm:"column(updater)"`
	Seq      int       `orm:"column(seq)"`
	Author   string    `orm:"column(author)"`
	Summary  string    `orm:"column(summary)"`
	Content  string    `orm:"column(content)"`
	Tags     string    `orm:"column(tags)"`
}

func (t *Article) TableName() string {
	return "cms_article"
}

func init() {
	orm.RegisterModel(new(Article))
}

func AddArticle(m *Article) (err error) {
	o := orm.NewOrm()
	_, err = o.Insert(m)
	return
}

//通过菜单id查找article
func GetArticleByMenu(menuId string) (articles []Article, err error) {
	o := orm.NewOrm()
	articles = make([]Article, 0)
	qs := o.QueryTable(new(Article)).Filter("IsDelete", 0).
		OrderBy("-Seq").Filter("menu_id", menuId)
	_, err = qs.All(&articles)
	return
}

func GetArticleById(id string) (v *Article, err error) {
	o := orm.NewOrm()
	v = &Article{Id: id}
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return v, err
}

func UpdateArticle(m *Article) (err error) {
	o := orm.NewOrm()
	v := Article{Id: m.Id}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Update(m); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}

func DeleteArticle(id string) (err error) {
	o := orm.NewOrm()
	v := Article{Id: id}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Delete(&Article{Id: id}); err == nil {
			fmt.Println("Number of records deleted in database:", num)
		}
	}
	return
}

func GetArticleByKeywords(keywords string) ([]Article, error) {
	o := orm.NewOrm()
	qs := o.QueryTable(new(Article))
	var list []Article
	_, err := qs.Filter("isdelete", 0).Filter("status", 1).Filter("name__icontains", keywords).OrderBy("ctime").
		RelatedSel("Menu").Limit(3).All(&list)
	if err != nil {
		return nil, err
	}
	return list, nil
}

func GetArticleKeywords(keywords string) ([]Article, error) {
	o := orm.NewOrm()
	qs := o.QueryTable(new(Article))
	var list []Article
	_, err := qs.Filter("isdelete", 0).Filter("status", 1).Filter("name__icontains", keywords).OrderBy("ctime").All(&list)
	if err != nil {
		return nil, err
	}
	return list, nil
}

func GetIndex(offset int64) ([]Article, error) {
	o := orm.NewOrm()
	qs := o.QueryTable(new(Article))
	var list []Article
	_, err := qs.Filter("isdelete", 0).Filter("status", 1).OrderBy("ctime").Limit(10).Offset(offset).All(&list)
	if err != nil {
		return nil, err
	}
	return list, nil
}
